package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.ProductListAdapter
import com.hinnotek.amoeba.bean.old.ProductListBean
import com.hinnotek.amoeba.databinding.IncludeRecyclerviewBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.ext.init
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.AmibaPayViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ProductListActivity : BaseActivity<IncludeRecyclerviewBinding>() {

    private var mAdapter: ProductListAdapter? = null
    private var customDialog: CustomDialog? = null

    val viewModel by viewModels<AmibaPayViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("产品")

        setContentView(R.layout.include_recyclerview)
    }

    override fun initVew() {
        bindingView.recyclerView.init()
        bindingView.recyclerView.adapter = ProductListAdapter().also { mAdapter = it }
        mAdapter?.setOnItemClickListener { adapter: BaseQuickAdapter<*, *>, view: View?, position: Int ->
            val productListData: ProductListBean = adapter.getItem(position) as ProductListBean
            val bundle = Bundle()
            bundle.putString("title", productListData.productName)
            bundle.putInt("productId", productListData.productId)
            IntentUtils.startActivity(mContext, ProductManagamentActivity::class.java, bundle)
        }
    }

    override fun setData() {
        customDialog = CustomDialog(mContext, "正在加载...")
        customDialog?.show()
        getProductList()
    }

    /**
     * app端获取全部产品接口
     */
    private fun getProductList(){
        viewModel.getProductList().observe(this,object : ObserverState<MutableList<ProductListBean>>(){
            override fun onDataChange(data: MutableList<ProductListBean>) {
                customDialog?.dismiss()
                mAdapter?.setList(data)
                super.onDataChange(data)
            }
            override fun onError(msg: String?, code: Int) {
                ToastUtils.showShort(mContext, msg)
                customDialog?.dismiss()
                super.onError(msg, code)
            }
        })
    }
}